package com.joysuch.wwyt.mh.service.impl;

import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.mh.bean.vo.MhMajorHazardPlanItemVO;
import com.joysuch.wwyt.mh.dto.MhMajorHazardPlanItemDTO;
import com.joysuch.wwyt.mh.entity.MhMajorHazardPlanItem;
import com.joysuch.wwyt.mh.mapper.MhMajorHazardPlanItemMapper;
import com.joysuch.wwyt.mh.service.MhMajorHazardPlanItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zqw
 * @since 2022-05-17
 */
@Service
public class MhMajorHazardPlanItemServiceImpl extends ServiceImpl<MhMajorHazardPlanItemMapper, MhMajorHazardPlanItem> implements MhMajorHazardPlanItemService {


    @Autowired
    private MhMajorHazardPlanItemMapper mhMajorHazardPlanItemMapper;

    @Override
    public void deleteByIds(List<Long> deleteItemIds) {
        if (CollectionUtils.isEmpty(deleteItemIds)) {
            return;
        }
        mhMajorHazardPlanItemMapper.deleteByIds(deleteItemIds);
    }

    @Override
    public List<MhMajorHazardPlanItem> saveItems(Long planId, List<MhMajorHazardPlanItemDTO> itemDtoList) {
        List<MhMajorHazardPlanItem> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(itemDtoList)) {
            return list;
        }
        for (MhMajorHazardPlanItemDTO dto : itemDtoList) {
            MhMajorHazardPlanItem item = new MhMajorHazardPlanItem();
            if (dto.getId() != null) {
                item = getById(dto.getId());
                if (item == null) {
                    continue;
                }
            }
            BeanUtils.copyProperties(dto, item);
            item.setPlanId(planId);
            list.add(item);
        }
        if (!CollectionUtils.isEmpty(list)) {
            saveOrUpdateBatch(list);
        }
        return list;
    }

    @Override
    public List<MhMajorHazardPlanItemVO> findByPlanId(Long planId,String deleteFlag) {
        List<MhMajorHazardPlanItemVO> voList = new ArrayList<>();
        List<MhMajorHazardPlanItem> itemList = mhMajorHazardPlanItemMapper.findByPlanId(planId,deleteFlag);
        if (!CollectionUtils.isEmpty(itemList)) {
            itemList.forEach(item -> {
                MhMajorHazardPlanItemVO itemVo = new MhMajorHazardPlanItemVO();
                BeanUtils.copyProperties(item, itemVo);
                voList.add(itemVo);
            });
        }
        return voList;
    }
}
